package com.mryang.hds.service.impl;

import com.mryang.hds.configs.AppException;
import com.mryang.hds.entity.Need;
import com.mryang.hds.entity.TripInfo;
import com.mryang.hds.entity.User;
import com.mryang.hds.mapper.TripInfoMapper;
import com.mryang.hds.mapper.UserMapper;
import com.mryang.hds.service.ITripInfoService;
import com.mryang.hds.utils.ResultCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Genius
 * @version 1.0.0
 * @ClassName TripInfoServiceImpl.java
 * @Description TODO
 * @createTime 2023年01月08日 12:18:00
 */

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class TripInfoServiceImpl implements ITripInfoService {

    @Resource
    private TripInfoMapper tripInfoMapper;
    @Resource
    private UserMapper userMapper;


    @Transactional
    @Override
    public void insertTripInfo(List<TripInfo> steps,Integer uId) {


        try {
            // 用户行程信息录入
            for (TripInfo step : steps) {
                tripInfoMapper.insertTripInfo(step,uId);
            }

            // 修改用户信息填报信息
            userMapper.updateUserTrip(uId);
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.TRIP_INFO_EXCEPTION);
        }

    }

    @Override
    public List<TripInfo> getTrips(User token) {
        try {
            return tripInfoMapper.getTrips(token);
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.TRIP_INFO_EXCEPTION);
        }
    }

    @Override
    public void addTrip(TripInfo tripInfo) {
        try {
            tripInfoMapper.addTrip(tripInfo);
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.TRIP_INFO_EXCEPTION);
        }
    }

    @Override
    public void editTrip(TripInfo tripInfo) {
        try {
            TripInfo tripOld = tripInfoMapper.getTripById(tripInfo.getTId());
            tripInfoMapper.editTrip(tripInfo,tripOld);
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.TRIP_INFO_EXCEPTION);
        }
    }

    @Override
    public void deleteTrip(Integer tid) {
        try {
            tripInfoMapper.deleteTrip(tid);
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.TRIP_INFO_EXCEPTION);
        }
    }

    @Override
    public List<TripInfo> getTripsByUId(Integer uid) {
        try {
            return tripInfoMapper.getTripsByUId(uid);
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.TRIP_INFO_EXCEPTION);
        }
    }



}
